package ev.initial;

import java.util.ArrayList;
import java.util.Random;

import TeamPursuit.MensTeamPursuit;
import TeamPursuit.SimulationResult;
import TeamPursuit.WomensTeamPursuit;

public class Initial {
	public static ArrayList<Individual> data = new ArrayList<Individual>();
	public static ArrayList<Individual> data2 = new ArrayList<Individual>();
	public Initial() {
		Random r = new Random();
		SimulationResult result = null;
        //WomensTeamPursuit womensTeamPursuit = new WomensTeamPursuit();
        MensTeamPursuit mensTeamPursuit = new MensTeamPursuit();
		//individual1's strategy and power sections are fixed
		//boolean [] strategy1 = {true, true, false, false, true, true, false, true, true, false, true, false, true, false, false, true, false, false, true, false, true, false};
		//int [] power1 = {830, 417, 395, 300, 237, 534, 339, 448, 314, 450, 336, 416, 332, 392, 288, 422, 326, 555, 471, 348, 657, 530, 216};
        //boolean [] strategy1 = {true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false};
        //int [] power1 = {400, 350, 350, 300, 300, 350, 350, 350, 350, 300, 300, 350, 350, 350, 350, 300, 300, 350, 350, 350, 350, 300, 300};
        //boolean [] strategy1 = {true, false, true, false, true, false, true, false, true, false, true, true, false, false, true, false, false, true, false, false, false, false};
        //int [] power1 = {920, 314, 969, 300, 466, 210, 350, 350, 360, 342, 306, 350, 350, 544, 269, 382, 651, 695, 201, 602, 675, 374, 255};
		//boolean [] strategy1 = {true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false};
		//int [] power1 = {400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400};
		boolean [] strategy1 = {true, false, true, false, true, false, true, false, true, false, true, true, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, false, false};
		int [] power1 = {707, 749, 460, 524, 804, 680, 476, 488, 358, 467, 842, 651, 922, 674, 550, 855, 550, 550, 550, 550, 900, 550, 550, 550, 678, 550, 807, 550, 646, 874, 455};
        
        
//        try {				
//			womensTeamPursuit.setHeight(0, 1.65);
//			womensTeamPursuit.setWeight(0, 70.0);
//			womensTeamPursuit.setMeanMaximumPower(0, 5.5);
//			
//			womensTeamPursuit.setHeight(2, 1.55);
//			womensTeamPursuit.setWeight(2, 60.0);
//			womensTeamPursuit.setMeanMaximumPower(2, 4.5);
//			
//			
//			// Simulate event with the default strategies
//			result = womensTeamPursuit.simulate(strategy1, power1);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}

		try {
			
			mensTeamPursuit.setHeight(0, 1.80);
			mensTeamPursuit.setWeight(0, 80.0);
			mensTeamPursuit.setMeanMaximumPower(0, 6.5);
			
			mensTeamPursuit.setHeight(2, 1.70);
			mensTeamPursuit.setWeight(2, 70.0);
			mensTeamPursuit.setMeanMaximumPower(2, 5.5);
			
			mensTeamPursuit.setHeight(3, 1.80);
			mensTeamPursuit.setWeight(3, 80.0);
			mensTeamPursuit.setMeanMaximumPower(3, 5.5);
			
			//result = mensTeamPursuit.simulate(DEFAULT_MENS_TRANSITION_STRATEGY, DEFAULT_MENS_PACING_STRATEGY);
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			result = mensTeamPursuit.simulate(strategy1, power1);
		} catch (Exception e3) {
			e3.printStackTrace();
		}
		int sLength=strategy1.length;
		int pLength=power1.length;
		
		//add individual1 into the population
		Individual indiv1=new Individual(strategy1,power1);
		indiv1.setTime(result.getFinishTime());
		data.add(indiv1);
		data2.add(indiv1);
		//the individual2's strategy section is fixed and power section is randomly generated 
		boolean [] strategy2 = {true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false};
	    int [] power2 = new int[pLength];
		for(int i=0;i<pLength;i++){
			//power2[i] = r.nextInt(101)+300;
			power2[i] = r.nextInt(201)+350;
		}
		
		try {
			result=mensTeamPursuit.simulate(strategy2, power2);
		} catch (Exception e2) {
			e2.printStackTrace();
		}
				
		//make sure the solution is valid generate four valid pacing with the same strategy for the use of women4 crossover
		for(int j =0; j<4; j++)
		{
			while(!isValid(result)){
				for(int i=0;i<pLength;i++){
					power2[i] = r.nextInt(201)+200;
					//power2[i] = r.nextInt(201)+350;
				}
				try {
					result=mensTeamPursuit.simulate(strategy2, power2);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			Individual indiv2=new Individual(strategy2,power2);
			indiv2.setTime(result.getFinishTime());
			data2.add(indiv2);
		}
			
		//add individual1 into the population
		Individual indiv2=new Individual(strategy2,power2);
		indiv2.setTime(result.getFinishTime());
		data.add(indiv2);		
		
		//the individual3's strategy and power sections are generated semi-randomly
		for(int j=0;j<8;j++){
			boolean [] strategy3 = new boolean[sLength];
			for(int i=0;i<sLength;i++){
				strategy3[i] = r.nextBoolean();
				if(i>1&&strategy3[i]==strategy3[i-1]&&strategy3[i]==strategy3[i-2]){
					if(strategy3[i]){
						strategy3[i]=false;
					}else{
						strategy3[i]=true;
					}
				}
			}
			int [] power3 = new int[pLength];
		    for(int i=0;i<pLength;i++){
				power3[i] = r.nextInt(601)+200;
				//power3[i] = r.nextInt(301)+300;
			}
		    try {
				result=mensTeamPursuit.simulate(strategy3, power3);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			while(!isValid(result)){
				for(int i=0;i<sLength;i++){
					strategy3[i] = r.nextBoolean();

				}

				for(int i=0;i<pLength;i++){
					power3[i] = r.nextInt(601)+200;
					//power3[i] = r.nextInt(301)+300;
				}
				try {
					result=mensTeamPursuit.simulate(strategy3, power3);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			Individual indiv3=new Individual(strategy3,power3);
			indiv3.setTime(result.getFinishTime());
			data.add(indiv3);
		}
	}
	private boolean isValid(SimulationResult result){
		if(result.getProportionCompleted() < 0.9999998){
			return false;
		}else{
			return true;
		}
	}

	public String toString() {
		String s = "population size: " + data.size() + "\n";
		for(int i=0;i<data.size();i++){
			s = s + "individual"+ i + ":" + "\n" + data.get(i).toString()+ "\n";
		}
		return s;
	}
	
}
